package redis

import (
	"com.yy.api/server/config"
	"context"

	rediz "github.com/redis/go-redis/v9"
)

type Operator struct {
	rds *rediz.Client
	ctx context.Context
}

func NewOperator() *Operator {
	rds := config.GetRedisClient()
	ctx := context.Background()
	return &Operator{
		rds: rds,
		ctx: ctx,
	}
}

func (o *Operator) GetStrValue(key string) (string, error) {

	val, err := o.rds.Get(o.ctx, key).Result()
	if err != nil {
		return "", err
	}
	return val, nil
}

func (o *Operator) SetStrValue(key string, value string) error {
	err := o.rds.Set(o.ctx, key, value, 0).Err()
	if err != nil {
		return err
	}
	return nil
}

func (o *Operator) DelStrValue(key string) error {
	err := o.rds.Del(o.ctx, key).Err()
	if err != nil {
		return err
	}
	return nil
}

func (o *Operator) ResetStrKV(key string, val string) error {
	if err := o.DelStrValue(key); err != nil {
		return err
	}
	if err := o.SetStrValue(key, val); err != nil {
		return err
	}
	return nil
}
